<?php

/*
*
*   Excalibur Content Management System
*   Copyright © 2008 Egor "Sontan" Kuryanovich
*
*   Based on Explay Engine v2.0 by Golovdinov Alexander
*
*   Official site: www.excms.ru
*   Contact e-mail: support@excms.ru
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
*/


header ('Cache-control: no-cache');

class template {
	
	var $path = '/modules/';
	var $error = '';
	var $user = '';
	var $auto_put_errors = false;
	var $auto_clear_vars = true;
	var $vars = Array ();
	var $out_vars = Array ();
	var $work_time = 0;
	var $count_ = 0;
	var $cache = array ();
	var $user_id = 0;
	var $author_id = 0;
	
	function __construct ($path) 
	{
		$this->path = $path;
	}
	
	function set_var ($start_var, $out_var) 
	{
		array_push ($this->vars, $start_var);
		array_push ($this->out_vars, $out_var);
	}
	
	function display ($module, $file, $auto_clear = true) 
	{
		$file = $module.'/views/'.$file;
		$time_before = 0;
		$time_before += $this->get_time();
		
		if (!$this->is_cached($file)) 
			$this->cache_file ($file);
		
		$file = $this->cache[$file];
		
		//print "<hr>$file</hr>";
		
		for ($i=0; $i < count($this->vars); $i++) 
			$file = str_replace ($this->vars[$i], $this->out_vars[$i], $file);
				
		$file = preg_replace ('#{=(.+?)=}#is', '', $file);
				
		if ($this->user == 'admin') 
		{
			$file = preg_replace ('#<unregister>(.+?)</unregister>#is', '', $file);
			$file = preg_replace ('#<register>(.+?)</register>#is', '\1', $file);
			$file = preg_replace ('#<admin>(.+?)</admin>#is', '\1', $file);
			$file = preg_replace ('#<moderator>(.+?)</moderator>#is', '\1', $file);
		}
		elseif ($this->user == 'moderator') 
		{
			$file = preg_replace ('#<unregister>(.+?)</unregister>#is', '', $file);
			$file = preg_replace ('#<admin>(.+?)</admin>#is', '', $file);
			$file = preg_replace ('#<register>(.+?)</register>#is', '\1', $file);
			$file = preg_replace ('#<moderator>(.+?)</moderator>#is', '\1', $file);
		}
		elseif ($this->user == 'register') 
		{
			$file = preg_replace ('#<unregister>(.+?)</unregister>#is', '', $file);
			$file = preg_replace ('#<admin>(.+?)</admin>#is', '', $file);
			$file = preg_replace ('#<register>(.+?)</register>#is', '\1', $file);
			$file = preg_replace ('#<moderator>(.+?)</moderator>#is', '', $file);
		}
		else 
		{
			$file = preg_replace ('#<unregister>(.+?)</unregister>#is', '\1', $file);
			$file = preg_replace ('#<admin>(.+?)</admin>#is', '', $file);
			$file = preg_replace ('#<register>(.+?)</register>#is', '', $file);
			$file = preg_replace ('#<moderator>(.+?)</moderator>#is', '', $file);
		}
		
		if ($this->user_id == $this->author_id || $this->user == 'admin' || $this->user == 'moderator') 
		{
			$file = preg_replace ('#<author>(.+?)</author>#is', '\1', $file);
		} 
		else 
		{
			$file = preg_replace ('#<author>(.+?)</author>#is', '', $file);
		}
				
		preg_match_all ('#<include=(.+?)>#is', $file, $include);
		for ($i=0; $i<count($include[0]); $i++) 
			$file = str_replace ('<include={$include[1][$i]}>', $this->get_file($include[1][$i]), $file);
				
		if ($auto_clear == true) 
			$this->clear_vars();
		
		if ($this->auto_put_errors == true && $this->error != '') 
			$this->errors();
		
		$this->work_time += $this->get_time() - $time_before;
		
		return $file;
		
	}
	
	function get_file ($file) 
	{
		$file = $_SERVER['DOCUMENT_ROOT'].'/'.$file;
		$time_before = 0;
		$this->time_before += $this->get_time();
		
		$file = str_replace ("'", '', $file);
		$file = str_replace ('"', '', $file);
		
		if (!file_exists($file)) 
		{
			$this->error .= "&#8212; Файл не существует: <strong>$file</strong>!<br />\n";
			$this->work_time += $this->get_time() - $this->time_before;
			return "<br /><strong>Template error:</strong> Файл не существует: <strong>$file</strong>!<br />\n";
		}
		elseif (file_exists($file)) 
		{
			
			if ($geting_file = file_get_contents($file)) 
			{
				$this->work_time += $this->get_time() - $time_before;
				return $geting_file;
			}
			else if ($this->path == '') 
			{
				$this->error .= "&#8212; Ошибка подгрузки файла: <strong>$file</strong>!<br />\n";
				$this->work_time += $this->get_time() - $time_before;
				return "<br /><strong>Template error:</strong> Ошибка подгрузки файла: <strong>$file</strong>!<br />\n";
			}
			
		}
	}
	
	function clear_vars () 
	{
		unset ($this->vars);
		unset  ($this->out_vars);
		$this->vars = Array ();
		$this->out_vars = Array ();
		$this->author_id = 0;
	}
	
	function errors () 
	{
		if ($this->path == '') 
			$this->error .= '&#8212; Не указан путь к папке с шаблонами!<br />';

		if ($this->user == '') 
			$this->error .= '&#8212; Не указана группа пользователя!<br />';
	
		if ($this->error != '') 
		{
			print "\n\n<div style=\"border: #FF0000 1px dashed; background-color: #FFDDDD; color: #BB0000; padding: 3px\">".
				"<strong>Template errors:</strong><p>\n".$this->error.
				"</div>\n\n";
		}
	}
	
	function past_work_time () 
	{
		return $this->work_time;
	}
	
	function get_time()
	{
		$this_time = microtime(); 
		$time_array = explode(" ",$this_time); 
		return ($time_array[1] + $time_array[0]); 
	}

	function is_cached ($name)  
	{
		if (isset($this->cache[$name])) 
			return true;
		else 
			return false;
	}
	
	function cache_file ($file) 
	{
		
		if (file_exists($this->path.$file)) 
		{
			$content = file_get_contents ($this->path.$file);
			$this->cache[$file] = $content;
		}
		else 
			echo "&#8212; Файл шаблона не существует: <strong>".$this->path.$file."</strong>!<br />\n";
		
		if ($this->auto_put_errors == true && $this->error != "") $this->errors();
		
		$this->count_++;
		
		return;
	}

}

